package com.ssm.maven.core.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ssm.maven.core.util.MD5Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ssm.maven.core.entity.PageBean;
import com.ssm.maven.core.entity.ProjectCase;
import com.ssm.maven.core.entity.ProjectCasesteps;
import com.ssm.maven.core.entity.User;
import com.ssm.maven.core.service.ProjectCaseService;
import com.ssm.maven.core.service.ProjectCasestepsService;
import com.ssm.maven.core.service.UserService;
import com.ssm.maven.core.util.ResponseUtil;
import com.ssm.maven.core.util.StringUtil;

/**
 * @author 1034683568@qq.com
 * @project_name ssm-maven
 * @date 2017-3-1
 */
@Controller
@RequestMapping("/projectcase")
public class ProjectcaseController {

    @Resource
    private ProjectCaseService projectcaseService;
    @Resource
    private ProjectCasestepsService projectcasestepService;
    private static final Logger log = Logger.getLogger(ProjectcaseController.class);// 日志文件

   

  

    /**
     * @param page
     * @param rows
     * @param s_user
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, ProjectCase pc, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (page != null && rows != null) {
            PageBean pageBean = new PageBean(Integer.parseInt(page),
                    Integer.parseInt(rows));
            map.put("start", pageBean.getStart());
            map.put("size", pageBean.getPageSize());
        }
        map.put("name", StringUtil.formatLike(pc.getName()));
        List<ProjectCase> PCList = projectcaseService.findPC(map);
        Long total = (long) projectcaseService.getTotalPC(map);
        System.out.println(total);
        JSONObject result = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(PCList);
        result.put("rows", jsonArray);
        result.put("total", total);
        log.info("request: user/list , map: " + map.toString());
        ResponseUtil.write(response, result);
        return null;
    }

    
    /**
     * 添加或修改用例
     *修改未做
     * @param response
     * @return
     * @throws Exception
     */
    
    @RequestMapping("/casesave")
    public String casesave(ProjectCase pc, HttpServletResponse response) throws Exception {
    	System.out.println(pc);
    	System.out.println(response);
        int resultTotal = 0;
//        pc.setId(100);
        if (pc.getId() == null) {
        	System.out.println(pc.getId());
            resultTotal = projectcaseService.insert(pc);
            System.out.println(resultTotal);
        } else {
            resultTotal = projectcaseService.updateByExample(null, null);
            System.out.println(resultTotal);
        }
        JSONObject result = new JSONObject();
        if (resultTotal > 0) {
            result.put("success", true);
            System.out.println(result);
        } else {
            result.put("success", false);
        }
        log.info("request: testcase/save , testcase: " + pc.toString());
        ResponseUtil.write(response, result);
        return null;
    }

    @RequestMapping("/show")
    public String show(@RequestParam(value = "testcase", required = false)String testcase,ProjectCasesteps pcs, HttpServletResponse response) throws Exception{
    	System.out.println(testcase);
    	
    	 List<ProjectCasesteps> PCSList = projectcasestepService.selectByForigenKeyname(testcase);
    	
         System.out.println(PCSList);
         ModelAndView mv =new ModelAndView();
         mv.addObject("abc", "bbbb");
    	
    	String a="dddd";
        JSONObject result = new JSONObject();
        if (a=="dddd") {
            result.put(PCSList, true);
//            System.out.println(result);
        } else {
            result.put("success", false);
        }	
        ResponseUtil.write(response, result);
        //其中第一个参数为url,第二个参数为要传递的数据的key,第三个参数为数据对象。

        //在这里要注意的是   数据是默认被存放在request中的。
        
return null;
//        return new ModelAndView("/ssm-maven/src/main/webapp/views/testcaseManage.jsp","path",path);

     }
}
